﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using hh = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using rz = functionService.serfun_city7.rz;
using nameCod = functionService.nameCod;


namespace city7
{
    enum repotrOst { ost, blank}
    enum akt { hCodd1, hCodd4, countCt1, countCt4}
    enum eg { n,on,p,pd,pa,r,rd,ra,ok,of,ct}
    public partial class FormAkt : Form
    {

        functionService.prm_city q;
        rzpt.modeWAED currentMode;
        DataTable dts;
        opro selectOpro;
        string sds, sdp, ds, dp, sm, ct; double oof;


        int linesPrint, currentPage;
        repotrOst currentReport;

        public FormAkt()
        {
            InitializeComponent(); InitializeComponentMy();
        }

        private void InitializeComponentMy()
        {
            vv.ost_sum_kol.sumTrue_kolFalse = false;
            q = new functionService.prm_city(); 

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);

            sds = q.tk(ETALON.param.period_s_5);      sdp = q.tk(ETALON.param.period_p_6);
            ds = Convert.ToDateTime(sds).Day.ToString();      dp = Convert.ToDateTime(sdp).Day.ToString();
            sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);
            loadAndSet();
            
        }

        void loadAndSet()
        {
            oof = 0;
            load();

            setMode(city7.rzpt.modeWAED.work);
            set(); setColor(Color.LightGray, Color.White);

            vv.rz.currentColumn = 0;
            vv.rz.currentRow = 0;

            selectOpro = opro.rp;
            currentReport = repotrOst.ost;
        }
       

        void cellclick()
        {
            if (currentMode == city7.rzpt.modeWAED.add)
            {
               
            }
            else if (currentMode == city7.rzpt.modeWAED.edit)
            {
                cellclick_edit();
            }
            else if (currentMode == city7.rzpt.modeWAED.delete)
            {
               
            }
            else if (currentMode == city7.rzpt.modeWAED.work)
            {
                cellclick_work();
            }
            else if (currentMode == city7.rzpt.modeWAED.non)
            {
               
            }
        }
        class gridf
        {
            private static string n;// = dataGridView1.Rows[yy].Cells[0].Value.ToString();
          private static double on;// = dataGridView1.Rows[yy].Cells[1].Value.ToString();
          private static double p;// = dataGridView1.Rows[yy].Cells[2].Value.ToString();
          private static double pd;// = dataGridView1.Rows[yy].Cells[3].Value.ToString();
          private static double pa;// = dataGridView1.Rows[yy].Cells[4].Value.ToString();
          private static double r;// = dataGridView1.Rows[yy].Cells[5].Value.ToString();
          private static double rd;// = dataGridView1.Rows[yy].Cells[6].Value.ToString();
          private static double ra;// = dataGridView1.Rows[yy].Cells[7].Value.ToString();
          private static double ok;// = dataGridView1.Rows[yy].Cells[8].Value.ToString();
          private static double of;// = dataGridView1.Rows[yy].Cells[9].Value.ToString();
          private static int ct;// = dataGridView1.Rows[yy].Cells[10].Value.ToString();


          #region prop
          public static string f00_n
          {
              get { return n; }
              set { n = value.ToString(); }
          }
          public static string f01_on
          {
              get { return String.Format("{0:0.00}", on); }
              set { n = value.ToString(); }
          }
          public static string f02_p
          {
              get { return String.Format("{0:0.00}", p); }
              set { p = Convert.ToDouble(value); }
          }
          public static string f03_pd
          {
              get { return String.Format("{0:0.00}", pd); }
              set { pd = Convert.ToDouble(value); }
          }
          public static string f04_pa
          {
              get { return String.Format("{0:0.00}", pa) ; }
              set
              {
                  pa = Convert.ToDouble(value);  //dataGridView1.Rows[yy].Cells[4].Value = pa; 
              }
          }
          public static string f05_r
          {
              get { return String.Format("{0:0.00}", r); }
              set { r = Convert.ToDouble(value); }
          }
          public static string f06_rd
          {
              get { return String.Format("{0:0.00}", rd); }
              set { rd = Convert.ToDouble(value); }
          }
          public static string f07_ra
          {
              get { return String.Format("{0:0.00}", ra);  }
              set
              {
                  ra = Convert.ToDouble(value);  //dataGridView1.Rows[yy].Cells[7].Value = pa;
              }
          }
          public static string f08_ok
          {
              get
              {
                  ok = on + p + pd + pa - r - rd - ra; 
                  //dataGridView1.Rows[yy].Cells[8].Value = ok;
                  return String.Format("{0:0.00}", ok) ;
              }
              set { ok = Convert.ToDouble(value); }
          }
          public static string f09_of_SET_ok
          {
              get { return String.Format("{0:0.00}", of);  }
              set { of = Convert.ToDouble( value); 
                  //dataGridView1.Rows[yy].Cells[9].Value = of; 
              para_set();
              ok_set();
              }
          }
          public static string f09_of
          {
              get { return String.Format("{0:0.00}", of); }
              set
              {
                  of = Convert.ToDouble(value);
              }
          }
          public static string f10_ct
          {
              get { return String.Format("{0:0.00}", ct) ; }
          }
          #endregion 


         static void  para_set()
         {
             double oks = on + p + pd - r - rd;
            
             ra = 0; pa = 0;
             if (oks - of < 0) { pa = -1 * (oks - of); }  else { ra = oks - of; }

             hh.m(hh.aa("oks=", oks, "  oks - of=", oks - of, "  pa=", pa, "  ra=", ra));
            
         }
         static void ok_set()
         {
             ok = on + p + pd + pa - r - rd - ra;
         }
        }


        private void cellclick_edit()
        {
            int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            object y = dataGridView1.SelectedCells[0].Value;
            int yy = dataGridView1.SelectedCells[0].RowIndex;
            object osc = dataGridView1.Rows[yy].Cells[0].Value;
                   
                   string  nct = dataGridView1.Rows[yy].Cells[0].Value.ToString();
                   ct = nameCod.readCod(nct);
            object ost = dataGridView1.Rows[yy].Cells[8].Value;
            object ostf = dataGridView1.Rows[yy].Cells[9].Value; //H.m(H.aa(ost, ostf));
            
            double o = Convert.ToDouble(ost), of = Convert.ToDouble(ostf);

            string sql1 = "", sql2="", kolFaktOst = "0";

        /*  hh.m(  string.Concat(" n=",  dts.Rows[yy][0],
              "  on=",  dts.Rows[yy][1],
              "  p=",  dts.Rows[yy][2],
              "  pd=",  dts.Rows[yy][3],
              "  pa=",  dts.Rows[yy][4],
              "  r=",  dts.Rows[yy][5],
              "  rd=",  dts.Rows[yy][6],
              "  ra=",  dts.Rows[yy][7],
              "  on=",  dts.Rows[yy][8],
              "  of=",  dts.Rows[yy][9],
              "  ct=",  dts.Rows[yy][10]));*/



            if (xx == 0)  //наименование сырья
            {
                #region  наименование сырья

                if (MessageBox.Show("Обнулить количество в актах по указанному товару", "Выбор", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string sql_dml1, sql_dml4;
                    object hasHk17Ras4d = q.selectScalar
                    #region  hasHk17Ras4d
(
                             string.Concat
                              (
                               "select count(*) as cr from",
                               " (",
                               " SELECT codd  FROM sir_rd_z s ",
                               " where codo=16 ",
                               " and left(codd,3)*1 = ", sm,
                               " and right(left(codd,5),2)*1 = ", dp,
                               " and right(left(codd,6),1)*1 = 4",
                               " and right(left(codd,7),2)*1 <> 13 ",
                               " )",
                               " as tt"
                               )
                    );
                    #endregion
                    object hasHk17Prh1d = q.selectScalar
                    #region  hasHk17Prh1d
(
                                    string.Concat
                                     (
                                      "select count(*) as cr from",
                                      " (",
                                      " SELECT codd  FROM sir_rd_z s ",
                                      " where codo=16 ",
                                      " and left(codd,3)*1 = ", sm,
                                      " and right(left(codd,5),2)*1 = ", dp,
                                      " and right(left(codd,6),1)*1 = 1",
                                      " and right(left(codd,7),2)*1 <> 13 ",
                                      " )",
                                      " as tt"
                                      )
                           );
                    #endregion
                    object countCt4d = q.selectScalar
                    #region  countCt4d
(

                              string.Concat
                                     (

                                     "(",
                           " select count(*) as cr from",
                            "(",
                            " SELECT r.codd as coddr, r.dd, r.nd, r.codo, s.*  FROM sir_td_z s",
                            " right join sir_rd_z r on s.codd=r.codd",
                            " where r.codo=16 ",
                            " and left(r.codd,3)*1 = ", sm,
                            " and right(left(r.codd,5),2)*1 = ", dp,
                            " and right(left(r.codd,6),1)*1 = 4",
                            " and right(left(r.codd,7),2)*1 <> 13 ",
                            " and ct = ", ct,
                            " )",
                            " as tt);"

                                     )
                        );
                    #endregion

                    object countCt1d = q.selectScalar
                    #region  countCt1d
(

                                string.Concat
                                       (

                                       "(",
                             " select count(*) as cr from",
                              "(",
                              " SELECT r.codd as coddr, r.dd, r.nd, r.codo, s.*  FROM sir_td_z s",
                              " right join sir_rd_z r on s.codd=r.codd",
                              " where r.codo=16 ",
                              " and left(r.codd,3)*1 = ", sm,
                              " and right(left(r.codd,5),2)*1 = ", dp,
                              " and right(left(r.codd,6),1)*1 = 1",
                              " and right(left(r.codd,7),2)*1 <> 13 ",
                              " and ct = ", ct,
                              " )",
                              " as tt);"

                                       )
                          );
                    #endregion


                    object codd4, codd1;

                    if (Convert.ToInt32(hasHk17Ras4d) > 0 && Convert.ToInt32(countCt4d) > 0)
                    {
                        codd4 = q.selectScalar(string.Concat
                              (
                               "SELECT codd  FROM sir_rd_z s ",
                               " where codo=16 ",
                               " and left(codd,3)*1 = ", sm,
                               " and right(left(codd,5),2)*1 = ", dp,
                               " and right(left(codd,6),1)*1 = 4;"
                               ));
                       // hh.m(codd4 );
                        sql_dml4 = string.Concat("UPDATE sir_td_z SET kol=0, kol2=0 where codd=" , codd4, 
                               " and ct = ", ct, ";");
                        q.UpdateData(sql_dml4);
                        dataGridView1.Rows[yy].Cells[7].Value = oof;
                    }
                    if (Convert.ToInt32(hasHk17Prh1d) > 0 && Convert.ToInt32(countCt1d) > 0)
                    {
                        codd1 = q.selectScalar(string.Concat
                              (
                               "SELECT codd  FROM sir_rd_z s ",
                               " where codo=16 ",
                               " and left(codd,3)*1 = ", sm,
                               " and right(left(codd,5),2)*1 = ", dp,
                               " and right(left(codd,6),1)*1 = 1",
                               " and right(left(codd,7),2)*1 <> 13;"
                               ));
                      //  hh.m(codd1);
                        sql_dml1 = string.Concat("UPDATE sir_td_z SET kol=0, kol2=0 where codd=", codd1,
                                " and ct = ", ct, ";");
                        q.UpdateData(sql_dml1);
                        dataGridView1.Rows[yy].Cells[4].Value = oof;
                    }
                }
                #endregion
            }
            else if (xx == 1) //ост начальные
            { }
            else if (xx == 2) //приход 
            { }
            else if (xx == 3) //приход др
            { }
            else if (xx == 4) //   приход  акт   
            { }
            else if (xx == 5) //расх
            {

            }
            else if (xx == 6) //расхдр
            {
              
            }
            else if (xx == 7) //расхАкт
            { }
            else if (xx == 8) //остк
            {
                
            }
            else if (xx == 9) //остф
            {
                #region  ост фактические
                ETALON.FormKolZakaz f = new ETALON.FormKolZakaz(); f.ShowDialog();
                if (f.IsSelect)
                {
                    kolFaktOst = f.valueEnter; 
                   // gridf.f09_of_SET_ok = kolFaktOst;

                    /*0-Наим
 1-остн
 2-прих
 3-прихдр
 4-прихакт
 5-расх
 6-расхдр
 7-расхАкт
 8-остк
 9-остф
 10-кодт*/



                    dataGridView1.SelectedCells[0].Value = kolFaktOst.Replace(".", ",");

                    oof = 
                        (Convert.ToDouble(dts.Rows[yy][1])
                       + Convert.ToDouble(dts.Rows[yy][2])
                       + Convert.ToDouble(dts.Rows[yy][3])
                       - Convert.ToDouble(dts.Rows[yy][5])
                       - Convert.ToDouble(dts.Rows[yy][6])) - Convert.ToDouble(kolFaktOst.Replace(".",","));
                  //  hh.m(oof);
                
                    /*oof =
                       (Convert.ToDouble(dataGridView1.Rows[yy].Cells[1].Value)
                      + Convert.ToDouble(dataGridView1.Rows[yy].Cells[2].Value)
                      + Convert.ToDouble(dataGridView1.Rows[yy].Cells[3].Value)
                      - Convert.ToDouble(dataGridView1.Rows[yy].Cells[5].Value)
                      - Convert.ToDouble(dataGridView1.Rows[yy].Cells[6].Value)) - Convert.ToDouble(kolFaktOst);*/



                    dataGridView1.Rows[yy].Cells[4].Value = 0;
                    dataGridView1.Rows[yy].Cells[7].Value = 0;
                    dataGridView1.Rows[yy].Cells[8].Value = kolFaktOst.Replace(".", ",");
                    dataGridView1.Rows[yy].Cells[9].Value = kolFaktOst.Replace(".", ",");

                    
                   // ViewState["Caption"] != null ? (string) ViewState["Caption"] : ""


                    
                    if (oof >= 0)
                    {
                       // createDocNkAktRashod_Prihod(4, yy);
                        createDocNkAkt(4, yy);
                        dataGridView1.Rows[yy].Cells[7].Value = oof;

                        //load(xx,yy);
                        //hh.m(hh.aa("Создаем расход ", oof));
                    }
                    else if (oof < 0)
                    {
                        oof = -oof;
                        createDocNkAkt(1, yy);
                        dataGridView1.Rows[yy].Cells[4].Value = oof;
                        
                    }

                    string s111 = string.Concat("call `hand_set123`('city.sir_td_z', 'cod123');");
                    q.UpdateData(s111);


                    sql1 = string.Concat("delete from citytemp.owd_ost_wd_", vv.ost_create.year_month_COD_203,
                        " where ct=", ct, ";");
                    sql2 = string.Concat("insert into citytemp.owd_ost_wd_", vv.ost_create.year_month_COD_203,
                        "(ct, name, osf)  values(", ct, ",'", nct, "',", 0, " );");

                    q.UpdateData(sql1);
                    q.UpdateData(sql2);
                }
                #endregion
            }
            else if (xx == 10) //ct
            { }
        }
        int countHkAktP
        {
            get
            {
                object hasHk17Prh1 = q.selectScalar
                #region  hasHk17Ras0
(
                                string.Concat
                                 (
                                  "select count(*) as cr from",
                                  " (",
                                  " SELECT codd  FROM sir_rd_z s ",
                                  " where codo=16 ",
                                  " and left(codd,3)*1 = ", sm,
                                  " and right(left(codd,5),2)*1 = ", dp,
                                  " and right(left(codd,6),1)*1 = 1",
                                  " and right(left(codd,7),2)*1 <> 13 ",
                                  " )",
                                  " as tt"
                                  )
                       );
                #endregion

                return Convert.ToInt32(hasHk17Prh1);
            }
        }
        int countHkAktR
        {
            get
            {
                object hasHk17Ras4 = q.selectScalar
                #region  hasHk17Ras
                                (
                                          string.Concat
                                           (
                                            "select count(*) as cr from",
                                            " (",
                                            " SELECT codd  FROM sir_rd_z s ",
                                            " where codo=16 ",
                                            " and left(codd,3)*1 = ", sm,
                                            " and right(left(codd,5),2)*1 = ", dp,
                                            " and right(left(codd,6),1)*1 = 4",
                                            " and right(left(codd,7),2)*1 <> 13 ",
                                            " )",
                                            " as tt"
                                            )
                                 );
                #endregion
                return Convert.ToInt32(hasHk17Ras4);
            }
        }
        int countCt1
        {
            get 
            {
                object countCt1 = q.selectScalar
                #region  countCt1
(

                          string.Concat
                                 (

                                 "(",
                       " select count(*) as cr from",
                        "(",
                        " SELECT r.codd as coddr, r.dd, r.nd, r.codo, s.*  FROM sir_td_z s",
                        " right join sir_rd_z r on s.codd=r.codd",
                        " where r.codo=16 ",
                        " and left(r.codd,3)*1 = ", sm,
                        " and right(left(r.codd,5),2)*1 = ", dp,
                        " and right(left(r.codd,6),1)*1 = 1",
                        " and right(left(r.codd,7),2)*1 <> 13 ",
                        " and ct = ", ct,
                        " )",
                        " as tt);"

                                 )
                    );
                #endregion
                return Convert.ToInt32( countCt1);
            }
        }
        int countCt4
        {
            get
            {
                object countCt4 = q.selectScalar
                #region  countCt4
(

                          string.Concat
                                 (

                                 "(",
                       " select count(*) as cr from",
                        "(",
                        " SELECT r.codd as coddr, r.dd, r.nd, r.codo, s.*  FROM sir_td_z s",
                        " right join sir_rd_z r on s.codd=r.codd",
                        " where r.codo=16 ",
                        " and left(r.codd,3)*1 = ", sm,
                        " and right(left(r.codd,5),2)*1 = ", dp,
                        " and right(left(r.codd,6),1)*1 = 4",
                        " and ct = ", ct,
                        " )",
                        " as tt);"

                                 )
                    );
                #endregion
                return Convert.ToInt32 (countCt4);
            }
        }

        


        private void createDocNkAkt(int i__4r__1p, int yy)
        {
            /*
             0-Наим
 1-остн
 2-прих
 3-прихдр
 4-прихакт
 5-расх
 6-расхдр
 7-расхАкт
 8-остк
 9-остф
 10-кодт
             */
            
           

            int i14 = 1, i41=4;
            int cp = countHkAktP, cr = countHkAktR, crp = 0, crpo=0, cc1 = countCt1, cc4 = countCt4, cc = 0, cco = 0;
            object codd = 0, coddo= 0;
            if (i__4r__1p == 1) 
            {
                i14 = 1; i41 = 4; cc = cc1; cco = cc4; crp = countHkAktP;crpo= countHkAktR; 
               // dataGridView1.Rows[yy].Cells[4].Value = oof; 
            }
            else 
            {
                i14 = 4; i41 = 1; cc = cc4; cco = cc1; crp = countHkAktR; crpo = countHkAktP;
               // dataGridView1.Rows[yy].Cells[7].Value = oof; 
            }

          //  dataGridView1.Rows[yy].Cells[8].Value = oof; 

           // hh.m(hh.aa("i14=", i14, "   cc1=", cc1, "   cc4=", cc4, "   crp=", crp, "   oof=", oof));


            if (cc  > 0) //-- есть документ и товар ct
            {
                // hh.m(hh.aa("countCt4=", countCt4, "   countCt1=", countCt1));
                codd = q.selectScalar
                #region codd
                (
                      string.Concat("select (codd14(", i14, ", ", sm, ", ", dp, ")) as codd;")
                );
                #endregion

                if (crpo > 0)
                {
                    coddo = q.selectScalar
                    #region codd
                    (
                          string.Concat("select (codd14(", i41, ", ", sm, ", ", dp, ")) as codd;")
                    );
                    #endregion
                    q.UpdateData(string.Concat("delete from sir_td_z where codd = ", coddo, " and ct = ", ct, ";"));
                }
                // hh.m(hh.aa("есть документ и товар ct", "codd4=", codd4));
                    q.UpdateData(string.Concat("delete from sir_td_z where codd = ", codd, " and ct = ", ct, ";"));
                    q.UpdateData(string.Concat
                                      (
                                      "insert into sir_td_z(cod123, ct, pr, kol, numcod, codd, posm, ndSkl, kol2)",
                                      " values ( 0, ", ct, ",  0,  1,  0,  ", codd, ",  0, 0,  ",
                                      String.Format("{0:0.0000000}", oof).Replace(",", "."), " );"
                                      )
                                 );
            }
            else
            {
                //-- есть документ нет товара
                //hh.m(hh.aa("есть документ нет товара", "hasHk17Ras=", hasHk17Ras, " codd=", codd));
                codd = q.selectScalar
                #region codd
                (
                      string.Concat("select (codd14(", i14, ", ", sm, ", ", dp, ")) as codd;")
                );
                #endregion

                if (crp > 0)
                {
                    // hh.m(hh.aa("11 есть документ", "codd=", codd));
                    q.UpdateData(string.Concat("delete from sir_td_z where codd = ", codd, " and ct = ", ct, ";"));

                    q.UpdateData(string.Concat("insert into sir_td_z( cod123, ct, pr, kol, numcod, codd, posm, ndSkl, kol2)",
                        " values( 0, ", ct, ", 0, 1, 0, ", codd, ", 0, 0, ", String.Format("{0:0.0000000}", oof).Replace(",", "."), " );"));
                }
                else
                {
                    //-- нет документа и нет товара
                    #region нет документа и нет товара
                    object countDoc1 = q.selectScalar(string.Concat
                    #region  countDoc1
(
                                  "(",
                                  " select count(codd) as cc ",
                                  " from city.sir_rd_z",
                                  " where",
                                  " left(codd,3)*1 = ", sm,
                                  " and right(left(codd,5),2)*1 = ", dp,
                                  " and right(left(codd,6),1)*1 = ", i14,
                                  " and right(left(codd,7),2)*1 <> 13 ",
                                  ");"
                              ));
                    #endregion
                    object MAXCODD17 = 0; //--((year_monthCurrent_203 * 100 + dayE) * 10 + 1) * 1000 + 1;
                    object MAXCCOD123R = q.selectScalar("select (max(COD123) + 1) as COD123 from city.sir_rd_z;");
                    if (Convert.ToInt32(countDoc1) > 0) // есть документы расхода
                    {

                        //-- находим максимальное значение codd 4 расхода

                        MAXCODD17 = q.selectScalar(string.Concat
                        #region MAXCODD17
(
                                   "(",
                                    "(",
                                     " select (max(codd) + 1) as mcodd17",
                                     " from city.sir_rd_z",
                                     " where",
                                     " left(codd,3)*1 = ", sm,
                                     " and right(left(codd,5),2)*1 = ", dp,
                                     " and right(left(codd,6),1)*1 = ", i14,
                                     " and right(left(codd,7),2)*1 <> 13 ",
                                    " )",
                                   " );"
                              )
                              );
                        #endregion
                    }
                    else
                    {
                        //(((sm * 100 + dayE) * 10 + 4) * 1000 + 1);
                        MAXCODD17 = ((hh.i(sm) * 100 + hh.i(dp)) * 10 + i14) * 1000 + 1;
                    }


                    //   -- добавляем документ в rd
                    string s12 = string.Concat
                       (
                             "insert into city.sir_rd_z(cod123, codd, codo, dd, codsotr, fpa, codkl, nd, coddSklad)",
                             " values (", MAXCCOD123R, ",", MAXCODD17, ", 16, (", DateTime.Now.Year,
                             "*100+ right(", sm, ", 2)*1 )*100+", dp, ", 4, 0, 0, 0, 0);"
                          
                        );
                    //hh.m(s12);
                    q.UpdateData(  s12 );

                    //  --  добавляем товар в td (и предварительно удаляем если есть)
                    // hh.m(hh.aa("добавляем товар в td (и предварительно удаляем если есть)", "codd=", codd));
                    q.UpdateData(string.Concat("delete from city.sir_td_z where codd = ", MAXCODD17, " and ct = ", ct, ";"));

                    string s11 = string.Concat("insert into city.sir_td_z(cod123, ct, pr, kol, numcod, codd, posm, ndSkl, kol2)",
                     " values( 0, ", ct, ",  0, 1, 0, ", MAXCODD17, ", 0, 0, ", String.Format("{0:0.0000000}", oof).Replace(",", "."), ");");
                    //hh.m(s11);
                    q.UpdateData(s11);
                    #endregion
                }


            }

        }

       
        
        private void cellclick_work()
        {
            try
            {
                int xx = dataGridView1.SelectedCells[0].ColumnIndex;
                object y = dataGridView1.SelectedCells[0].Value;
                int yy = dataGridView1.SelectedCells[0].RowIndex;
                object osc = dataGridView1.Rows[yy].Cells[0].Value;
                string ct = nameCod.readCod(dataGridView1.Rows[yy].Cells[0].Value.ToString());
            
            
            string sql = "";

            if (xx == 0)  //наименование сырья
            { }  
            else if (xx == 1) //ост начальные
            { }
            else if (xx == 2) //приход до нач периода
            {
                /*`city`.`ost_prih_beforeWorkDay_viewDoc_ct`(
    year_monthCurrent_203 int,
    dayB int, ct0 int,
    LOOK_1_DONTLOOK_0 INT)*/
              //  hh.m(hh.aa("приход до нач периода", ct));
                city7.FormDocLook f = new FormDocLook((int)opro.p, ct); f.ShowDialog();
            }
            else if (xx == 3) //расход до нач периода
            {
               // hh.m(hh.aa("расход до нач периода", ct));
                city7.lists.FormFilters ff = new city7.lists.FormFilters(2); ff.ShowDialog();
                if (ff.IsOk)
                {
                    if (nameCod.readCod(ff.valueSelect) == "1") // doc
                    { selectOpro = opro.look_doc_r; }
                    else if (nameCod.readCod(ff.valueSelect) == "2") // all sirie
                    { selectOpro = opro.look_All_rezptBPeriod ; }
                    else if (nameCod.readCod(ff.valueSelect) == "3") // sir po ct
                    { selectOpro = opro.look_All_rezpt_BPeriod_ct; }
                    else if (nameCod.readCod(ff.valueSelect) == "4") // sir po ct
                    { selectOpro = opro.look_doc_r; }

                    //hh.m(selectOpro," ", ct);
                    city7.FormDocLook f = new FormDocLook((int)selectOpro, ct); f.ShowDialog();

                }


            }
            else if (xx == 4) //ост нач периода
            { }
            else if (xx == 5) //приход за период
            {
               // hh.m(hh.aa("приход за период", ct));
                city7.FormDocLook f = new FormDocLook((int)opro.pp, ct); f.ShowDialog();
            }
            else if (xx == 6) //расход за период
            {
               // hh.m(hh.aa("расход за период", ct));

                city7.lists.FormFilters ff = new city7.lists.FormFilters(2); ff.ShowDialog();
                if (ff.IsOk)
                {
                    if( nameCod.readCod( ff.valueSelect) == "1") // doc
                    { selectOpro = opro.look_doc_rp;  }
                    else if (nameCod.readCod( ff.valueSelect) == "2") // all sirie
                    { selectOpro = opro.look_All_rezpt;  }
                    else if (nameCod.readCod(ff.valueSelect) == "3") // sir po ct
                    { selectOpro = opro.look_All_rezpt_ct  ; }
                    city7.FormDocLook f = new FormDocLook((int)selectOpro, ct); f.ShowDialog();

                }


            }
            else if (xx == 7) //ост конечные
            { }
            else if (xx == 8) //ост фактические
            { }
            else if (xx == 9) //fpa
            { }
            else if (xx == 10) //ct
            { }
            }
            catch { }
        }

        void set()
        {
            int ii = 96, ij=85, jj = 90, kk=1;
           // ostn, pp, pakt,rp,rakt,ostk


            string[] aa = new string[]
                { "Наименование", "ОстН", "Прих", "ПрихДр","ПрихАкт","Расх", "РасхДр", "РасхАкт","ОстК", "ОстФ","КодТ" };
            string[] sa = new string[] 
                { 
                "Наименование", "ЦенаОН", "Цена", "ЦенаДрЛин",
                "ОстН", "Сумма",
                "Прих", "Сумма", 
                "ПрихДр", "Сумма",
                "ПрихАкт", "Сумма", 
                "Расх", "Сумма", 
                "РасхДр", "Сумма", 
                "РасхАкт", "Сумма",
                "ОстК", "Сумма" 
                };


            if (vv.ost_sum_kol.sumTrue_kolFalse == false)
            {
                #region  kol
                if (currentMode == city7.rzpt.modeWAED.work)
                {

                    for (int j = 0; j < aa.Length; j++)
                    {
                        if (j == 0) { dataGridView1.Columns[j].Width = 250; }
                        else if (j == 10)
                        {
                            dataGridView1.Columns[j].Width = 50;
                            dataGridView1.Columns[j].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
                            dataGridView1.Columns[j].DefaultCellStyle.Format = "F0";
                        }
                        else
                        {
                            dataGridView1.Columns[j].Width = ii;

                            dataGridView1.Columns[j].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            dataGridView1.Columns[j].DefaultCellStyle.Format = "F2";
                        }

                        dataGridView1.Columns[j].HeaderText = aa[j];
                    }

                }
                else if (currentMode == city7.rzpt.modeWAED.edit)
                {
                    for (int j = 0; j < aa.Length ; j++)
                    {
                        if (j == 0) { dataGridView1.Columns[j].Width = 250; }
                        else if (j == 7 || j == 8 || j == 9 || j == 4)
                        {
                            dataGridView1.Columns[j].Width = ii;
                            dataGridView1.Columns[j].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            dataGridView1.Columns[j].DefaultCellStyle.Format = "F2";
                        }
                        else
                        {
                            dataGridView1.Columns[j].Width = 0;


                        }

                        dataGridView1.Columns[j].HeaderText = aa[j];
                    }
                }
                #endregion
            }
            else 
            {
                if (currentMode == city7.rzpt.modeWAED.work)
                {

                    for (int j = 0; j < sa.Length; j++)
                    {
                        if (j == 0) { dataGridView1.Columns[j].Width = 250; }
                        else if (j == 1 || j == 2 || j == 3)
                        {
                            dataGridView1.Columns[j].Width = ij;

                            dataGridView1.Columns[j].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            dataGridView1.Columns[j].DefaultCellStyle.Format = "F5";
                        }
                        else
                        {
                            dataGridView1.Columns[j].Width = ij;

                            dataGridView1.Columns[j].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            dataGridView1.Columns[j].DefaultCellStyle.Format = "F2";
                        }

                        dataGridView1.Columns[j].HeaderText = sa[j];
                    }
                }
            }
        }
        void setMode(rzpt.modeWAED mo)
        {
            currentMode = mo;
            if (mo == city7.rzpt.modeWAED.work)
            {
                set(); setColor(Color.LightGray, Color.White); 
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
               // refresh();
            }
            else if (mo == city7.rzpt.modeWAED.add)
            {
               
            }
            else if (mo == city7.rzpt.modeWAED.edit)
            {
                set(); setColor(Color.LightGreen, Color.LightSkyBlue);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            }
            else if (mo == city7.rzpt.modeWAED.delete)
            {
              
            }
            else if (mo == city7.rzpt.modeWAED.non)
            {
               
            }

        }
        void setColor(Color c1, Color c2)
        {
            if (vv.ost_sum_kol.sumTrue_kolFalse == false)
            {
                dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[1].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[3].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[5].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[6].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[7].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[8].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[9].DefaultCellStyle.BackColor = c2;
            }
            else 
            {
                for (int j = 0; j < 18; j++)
                {
                     if (j == 0 || j == 1 ||j == 3 || j == 5 || j == 7 || j == 9 ||
                         j == 11 || j == 13 || j == 15 || j == 17)
                    {
                        dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                    }
                    else
                    {
                        dataGridView1.Columns[0].DefaultCellStyle.BackColor = c2;
                    }
                }
            }
            //dataGridView1.Columns[9].DefaultCellStyle.BackColor = c1;
           // dataGridView1.Columns[10].DefaultCellStyle.BackColor = c1;
        }

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.work); refresh();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.edit);
        }
        void refresh(string whereName)
        {
            if (whereName != null && whereName.Length > 0)
            {
                dts = q.selectAny(string.Concat("SELECT * FROM citytemp.owd_ost_wd WHERE name rlike('", whereName, "') ORDER BY name;"));
                dataGridView1.DataSource = dts;
            }
            else
            {
                refresh();
            }
        }
        private void toolStripTextBox1KeyUp()
        {
           //  q.sh(dts);
            string sf = toolStripTextBox1.Text;
            if (vv.ost_sum_kol.sumTrue_kolFalse == false )
            {
                setItog_OST_kol_filter(dts, sf);
            }
            else
            {
                setItog_filter(dts, sf);
            }
           //  q.sh(dts);
            DataView dv = new DataView(dts); //H.m(30);

            dv.RowFilter = string.Concat("name like '%", sf, "%' or name like '%ИТОГИ:%'"); //q.sh(dv);
            dataGridView1.DataSource = dv;  
        }
       // toolStripTextBox1KeyUp()
        void refresh(int xxx, int yyy)
        {
            refresh();
            dataGridView1.CurrentCell = dataGridView1[xxx, yyy];
        }
        void refresh()
        {
          //  dts = q.selectAny(string.Concat("SELECT * FROM ost_wd ORDER BY name;"));
            dataGridView1.DataSource = dts;
            //dataGridView1.CurrentCell = dataGridView1[xxx, yyy];
        
        }
        private void updateAVG_Pr()
        {
            string sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);
            // CALL CITY.AVGP_create_table_avgPr(112, 0);
            q.UpdateData(string.Concat("call city.AVGP_create_table_avgPr(", sm, ", 0 )"));
            q.UpdateData(string.Concat("call city.avgPr_prep_coddAkt(", sm, ", 0 )"));
        }
        void load()
        {
            string sds = q.tk(ETALON.param.period_s_5),
                sdp = q.tk(ETALON.param.period_p_6),
                ds = Convert.ToDateTime(sds).Day.ToString(),
                dp = Convert.ToDateTime(sdp).Day.ToString(),
                sm = q.tk(ETALON.param.kafe_smena_42).Substring(0,3);
                                                        //ost_workDay_s2_1
            //int ii=0;
            string sql_dml = "";

            if (vv.ost_sum_kol.sumTrue_kolFalse == false)
            {
                if (MessageBox.Show("Пересчитать остатки", "Выбор",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {   
                    updateAVG_Pr();
                    sql_dml = string.Concat("call `city`.`ost_workDay_s2_1_akt` (", sm, ", ", ds, ",", dp, ", 3, 1);"); //hh.m(sql_dml);
                    dts = q.selectAny(sql_dml);
                   
                }
                else 
                {
                    dts = q.selectAny(string.Concat("SELECT * FROM citytemp",Month01,".owd_ost_wd_filt ORDER BY name;"));
                }
                setItog_OST_kol_filter(dts,")");
            }
            else 
            {
                dts = q.selectAny(string.Concat("call `city`.`ost_avgPr`(", sm, ");"));
                dataGridView1.DataSource = null;
                //setItog(dts);
                setItog_filter(dts, ")");
            }
            
            dataGridView1.DataSource = dts;
          //  q.sh(dts);

        }


        private void delRows(DataTable dto, object sWhatDEL, int indexColumn)
        {
            ArrayList rowsToRemove = new ArrayList();
            for (int i = 0; i < dto.Rows.Count; i++)
            {
                if (dto.Rows[i][indexColumn].ToString() == sWhatDEL)
                    rowsToRemove.Add(dto.Rows[i]);
            }

            for (int j = 0; j < rowsToRemove.Count; j++)
            {
                dto.Rows.Remove((DataRow)rowsToRemove[j]);
            }
        }

        private void setItog_filter(DataTable dto, string sfilter)
        {
            string flt = string.Concat("name like '%", sfilter, "%'");
            /*SELECT name
       , pr
       , ostn
       , ostn * pr AS ostns
       , pp
       , pp * pr AS pps
       , pol
       , pol * pr AS pols
       , pakt
       , pakt * pr AS pakts
       , rp
       , rp * pr AS rps
       , rol
       , rol * pr AS rols
       , rakt
       , rakt * pr AS rakts
       , ostk
       , ostk * pr AS ostks
       , ct
  FROM
    o9;*/
            delRows(dto, "ИТОГИ:", 0);
            int ic = dto.Rows.Count;
            DataRow r = dto.NewRow();
            // String.Format("{0:0.00}", pd);
            //dts.Rows.Remove(
            r[(int)enumR.table_ostSum._00_name] = "ИТОГИ:";
            try
            {
                r[(int)enumR.table_ostSum._04_ostn ] = string.Format("{0:0}", dto.Compute("sum(ostn)", flt));
                r[(int)enumR.table_ostSum._05_ostns ] = string.Format("{0:0.00}", dto.Compute("sum(ostns)", flt));

                r[(int)enumR.table_ostSum._06_pp  ] = string.Format("{0:0}", dto.Compute("sum(pp)", flt));
                r[(int)enumR.table_ostSum._07_pps ] = string.Format("{0:0.00}", dto.Compute("sum(pps)", flt));

                r[(int)enumR.table_ostSum._08_pol ] = string.Format("{0:0}", dto.Compute("sum(pol)", flt));
                r[(int)enumR.table_ostSum._09_pols ] = string.Format("{0:0.00}", dto.Compute("sum(pols)", flt));

                r[(int)enumR.table_ostSum._10_pakt] = string.Format("{0:0}", dto.Compute("sum(pakt)", flt));
                r[(int)enumR.table_ostSum._11_pakts] = string.Format("{0:0.00}", dto.Compute("sum(pakts)", flt));

                r[(int)enumR.table_ostSum._12_rp] = string.Format("{0:0}", dto.Compute("sum(rp)", flt));
                r[(int)enumR.table_ostSum._13_rps] = string.Format("{0:0.00}", dto.Compute("sum(rps)", flt));

                r[(int)enumR.table_ostSum._14_rol] = string.Format("{0:0}", dto.Compute("sum(rol)", flt));
                r[(int)enumR.table_ostSum._15_rols] = string.Format("{0:0.00}", dto.Compute("sum(rols)", flt));

                r[(int)enumR.table_ostSum._16_rakt] = string.Format("{0:0}", dto.Compute("sum(rakt)", flt));
                r[(int)enumR.table_ostSum._17_rakts] = string.Format("{0:0.00}", dto.Compute("sum(rakts)", flt));

                r[(int)enumR.table_ostSum._18_ostk] = string.Format("{0:0}", dto.Compute("sum(ostk)", flt));
                r[(int)enumR.table_ostSum._19_ostks] = string.Format("{0:0.00}", dto.Compute("sum(ostks)", flt));
                r[(int)enumR.table_ostSum._20_ct] = 888;
                dto.Rows.Add(r);
                dto.AcceptChanges();
            }
            catch { }

        }

        private void setItog_OST_kol_filter(DataTable dto, string sfilter)
        {
            string flt = string.Concat("name like '%", sfilter, "%' or name = 'ИТОГИ:'");
            /**/
            delRows(dto, "ИТОГИ:", 0);
            int ic = dto.Rows.Count;
            DataRow r = dto.NewRow();
            // String.Format("{0:0.00}", pd);
            //dts.Rows.Remove(
            r[(int)enumR.table_ostSum._00_name] = "ИТОГИ:";
            try
            {
                r[(int)enumR.table_owd_ost_wd_filt._01_ostn ] = string.Format("{0:0}", dto.Compute("sum(ostn)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._02_pp] = string.Format("{0:0}", dto.Compute("sum(pp)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._03_pol] = string.Format("{0:0}", dto.Compute("sum(pol)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._04_pakt] = string.Format("{0:0}", dto.Compute("sum(pakt)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._05_rp] = string.Format("{0:0}", dto.Compute("sum(rp)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._06_rol] = string.Format("{0:0}", dto.Compute("sum(rol)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._07_rakt] = string.Format("{0:0}", dto.Compute("sum(rakt)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._08_ostk] = string.Format("{0:0}", dto.Compute("sum(ostk)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._09_osf] = string.Format("{0:0}", dto.Compute("sum(osf)", flt));
                r[(int)enumR.table_owd_ost_wd_filt._10_ct] = 888;

                
                dto.Rows.Add(r);
                dto.AcceptChanges();
            }
            catch { }

        }


        private void  setItog(DataTable dto)
        {
            /*SELECT name
       , pr
       , ostn
       , ostn * pr AS ostns
       , pp
       , pp * pr AS pps
       , pol
       , pol * pr AS pols
       , pakt
       , pakt * pr AS pakts
       , rp
       , rp * pr AS rps
       , rol
       , rol * pr AS rols
       , rakt
       , rakt * pr AS rakts
       , ostk
       , ostk * pr AS ostks
       , ct
  FROM
    o9;*/
            int ic = dto.Rows.Count;
            DataRow r = dto.NewRow();
           // String.Format("{0:0.00}", pd);
            r[(int)enumR.table_ostSum._00_name] = "ИТОГИ:";
            r[(int)enumR.table_ostSum._05_ostns] = string.Format("{0:0.00}", dto.Compute("sum(ostns)", "ct>0"));
            r[(int)enumR.table_ostSum._07_pps] = string.Format("{0:0.00}", dto.Compute("sum(pps)", "ct>0"));
            r[(int)enumR.table_ostSum._09_pols] = string.Format("{0:0.00}", dto.Compute("sum(pols)", "ct>0"));
            r[(int)enumR.table_ostSum._11_pakts] = string.Format("{0:0.00}", dto.Compute("sum(pakts)", "ct>0"));
            r[(int)enumR.table_ostSum._13_rps] = string.Format("{0:0.00}", dto.Compute("sum(rps)", "ct>0"));
            r[(int)enumR.table_ostSum._15_rols] = string.Format("{0:0.00}", dto.Compute("sum(rols)", "ct>0"));
            r[(int)enumR.table_ostSum._17_rakts] = string.Format("{0:0.00}", dto.Compute("sum(rakts)", "ct>0"));
            r[(int)enumR.table_ostSum._19_ostks] = string.Format("{0:0.00}", dto.Compute("sum(ostks)", "ct>0"));
            r[(int)enumR.table_ostSum._20_ct ] = 888;
            dto.Rows.Add(r);
            dto.AcceptChanges();
            
        }
        private string Month01
        {
            get
            {
                string s = q.tk(5);
                DateTime dmy = Convert.ToDateTime(s); return hh.nn(dmy.Month);
            }
        }
        void load(int xxx, int yyy)
        {
            load(); dataGridView1.CurrentCell = dataGridView1[xxx, yyy];
        }
        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //if (toolStripTextBox1.Text.Length > 0)
            //{ refresh(toolStripTextBox1.Text); }
            //else {  refresh(); }
            toolStripTextBox1KeyUp();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
          //  cellclick();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cellclick();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            RPT_ost();
        }
        private void RPT_ost()
        {
            currentPage = 0;
            
            string sds = q.tk(ETALON.param.period_s_5),
               sdp = q.tk(ETALON.param.period_p_6),
               ds = Convert.ToDateTime(sds).Day.ToString(),
               dp = Convert.ToDateTime(sdp).Day.ToString(),
               sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);
           
            dts = q.selectAny(string.Concat("call `city`.`ost_workDay_s2_1` (", sm, ", ", ds, ",", dp, ", 1, 0);"));
            vv.RPT_ost_look.currentReport  = vv.RPT_ost_look.report_ost;
            printDocument1.DefaultPageSettings.Landscape = false ;
            printPreviewDialog1.ShowDialog();
        }
        private void RPT_ostS()
        {
            currentPage = 0;

            string sds = q.tk(ETALON.param.period_s_5),
               sdp = q.tk(ETALON.param.period_p_6),
               ds = Convert.ToDateTime(sds).Day.ToString(),
               dp = Convert.ToDateTime(sdp).Day.ToString(),
               sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);

            dts = q.selectAny(string.Concat("call `city`.`ost_avgPr` (", sm, ");"));
            vv.RPT_ost_look.currentReport = vv.RPT_ost_look.report_ost_sum;
            printDocument1.DefaultPageSettings.Landscape = true;
            printPreviewDialog1.ShowDialog();
        }
        private void RPT_ost_blank()
        {
            currentPage = 0;

            string sds = q.tk(ETALON.param.period_s_5),
               sdp = q.tk(ETALON.param.period_p_6),
               ds = Convert.ToDateTime(sds).Day.ToString(),
               dp = Convert.ToDateTime(sdp).Day.ToString(),
               sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);

            dts = q.selectAny(string.Concat("call `city`.`ost_workDay_s2_1` (", sm, ", ", ds, ",", dp, ", 1, 0);"));
            vv.RPT_ost_look.currentReport = vv.RPT_ost_look.report_ost_blank;
            printDocument1.DefaultPageSettings.Landscape = false;
            printPreviewDialog1.ShowDialog();
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            RPT_ost_blank();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (vv.RPT_ost_look.currentReport == vv.RPT_ost_look.report_ost)
            {
                ost_print(e);
            }
            else if (vv.RPT_ost_look.currentReport == vv.RPT_ost_look.report_ost_blank)
            {
                ost_blank_print(e);
            }
            else if (vv.RPT_ost_look.currentReport == vv.RPT_ost_look.report_ost_sum)
            {
               
                ostSum_blank_print(e);
            }
        }

        private string nn(object o)
        {
            string s = "0";
            if (Convert.ToInt32(o) < 10) { s = string.Concat("0", o); } else { s = o.ToString(); }
            return s;
        }
        private string n(object o)
        {
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-") { s = o.ToString(); }
            return s;
        }
        private string d(object o)
        {
            double d, dd = 0; ; int id; int k = 10 * 1;
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-")
            {
                s = o.ToString();
                d = Convert.ToDouble(s) * k; 
                id = Convert.ToInt32(d); 
                dd = Convert.ToDouble(id) / k; 
                s = String.Format("{0:0.0}", dd); ;

            }
            return s;
        }
        private string d5(object o)
        {
            double d, dd = 0; ; int id; int k = 10 * 1;
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-")
            {
                s = o.ToString();
                d = Convert.ToDouble(s) * k;
                id = Convert.ToInt32(d);
                dd = Convert.ToDouble(id) / k;
                s = String.Format("{0:0.00000}", dd); ;

            }
            return s;
        }
        private string d(object o, object  decCount0)
        {
            int decCount = Convert.ToInt32(decCount0);
            double dv, dd = 0; ; int id; int k = 10 * 1;
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-")
            {
                s = o.ToString();
                dv = Convert.ToDouble(s) * k;
                id = Convert.ToInt32(dv);
                dd = Convert.ToDouble(id) / k;
                if (decCount == 1)
                {
                    s = String.Format("{0:0.0}", dd);
                }
                else if (decCount == 2)
                {
                    s = String.Format("{0:0.00}", dd); 
                }
                else if (decCount == 3)
                {
                    s = String.Format("{0:0.000}", dd);
                }
                else if (decCount == 4)
                {
                    s = String.Format("{0:0.0000}", dd);
                }
                else if (decCount == 5)
                {
                    s = String.Format("{0:0.00000}", dd);
                }
                else if (decCount == 6)
                {
                    s = String.Format("{0:0.000000}", dd);
                }
                else if (decCount == 7)
                {
                    s = String.Format("{0:0.0000000}", dd);
                }
            }
            return s;
        }
        private double  d(object o,int int10umnogit)
        {
            double d, dd = 0; ; int id; int k = 10 * int10umnogit;
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-") 
            { 
                s = o.ToString();
                d = Convert.ToDouble(s) * k; id = Convert.ToInt32(d); dd = Convert.ToDouble(id) / k;
               
            }
            return dd;
        }
        private void ost_print_sum(System.Drawing.Printing.PrintPageEventArgs e)
        {

           

            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            Rectangle r0;
            Rectangle r1, r2, r3, r4, r5,
                      r6,r7,r8,
                      r9,r10;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 220, ww = 30, wk=55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6=wk,w7=wk,w8=wk+10,
                w9=wk, w10=15;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Остатки по цеху за период c ", q.tk(ETALON.param.period_s_5 )," по " , q.tk(ETALON.param.period_p_6));
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;
           
            //---------------------- Итоги
            #region r1---r21

           
            #endregion

            #region drawStr
            //g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            //g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            //g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r0 = new Rectangle(x, y, w0, h);
            r1 = new Rectangle(x + w0 , y, w1, h);
            r2 = new Rectangle(x + w0 + w1 , y, w2, h);
            r3 = new Rectangle(x + w0 + w1 + w2 , y, w3, h);
            r4 = new Rectangle(x + w0 + w1 + w2 + w3 , y, w4, h);
            r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 , y, w5, h);
            r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 , y, w6, h);
            r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 , y, w7, h);
            r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 , y, w8, h);
            r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 , y, w9, h);
            r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


            g.DrawRectangle(Pens.LightSteelBlue, r0);
            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);
            g.DrawRectangle(Pens.LightSteelBlue, r6);
            g.DrawRectangle(Pens.LightSteelBlue, r7);
            g.DrawRectangle(Pens.LightSteelBlue, r8);
            g.DrawRectangle(Pens.LightSteelBlue, r9);
            g.DrawRectangle(Pens.LightSteelBlue, r10);


            #endregion

            #region drawStr
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r0, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("ОстН", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("Прих", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("Расх", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("ОстНП", new Font("Arial", f), Brushes.Black, r5, sfc);
            g.DrawString("ПрихП", new Font("Arial", f), Brushes.Black, r6, sfc);
            g.DrawString("РасхП", new Font("Arial", f), Brushes.Black, r7, sfc);
            g.DrawString("ОстКП", new Font("Arial", f), Brushes.Black, r8, sfc);
            g.DrawString("ОстФ", new Font("Arial", f), Brushes.Black, r9, sfc);
            g.DrawString("РНС", new Font("Arial", f), Brushes.Black, r10, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {
              
                        #region r1---r21

                r0 = new Rectangle(x, y, w0, h);
                r1 = new Rectangle(x + w0, y, w1, h);
                r2 = new Rectangle(x + w0 + w1, y, w2, h);
                r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
                r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
                r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
                r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
                r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
                r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
                r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


                g.DrawRectangle(Pens.LightSteelBlue, r0);
                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);
                g.DrawRectangle(Pens.LightSteelBlue, r3);
                g.DrawRectangle(Pens.LightSteelBlue, r4);
                g.DrawRectangle(Pens.LightSteelBlue, r5);
                g.DrawRectangle(Pens.LightSteelBlue, r6);
                g.DrawRectangle(Pens.LightSteelBlue, r7);
                g.DrawRectangle(Pens.LightSteelBlue, r8);
                g.DrawRectangle(Pens.LightSteelBlue, r9);
                g.DrawRectangle(Pens.LightSteelBlue, r10);
                        #endregion

                        #region drawStr
                        //g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                        //g.DrawString(String.Format("{0:0.00}", n(dts.Rows[linesPrint][0])), new Font("Arial", f), Brushes.Black, r1);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][1])), new Font("Arial", f), Brushes.Black, r2, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][2])), new Font("Arial", f), Brushes.Black, r3, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][3])), new Font("Arial", f), Brushes.Black, r4, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][4])), new Font("Arial", f), Brushes.Black, r5, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][5])), new Font("Arial", f), Brushes.Black, r6, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][6])), new Font("Arial", f), Brushes.Black, r7, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][7])), new Font("Arial", f), Brushes.Black, r8, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][8])), new Font("Arial", f), Brushes.Black, r9, sfo);
                        //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][9])), new Font("Arial", f), Brushes.Black, r10);



                        g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                        g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r1);
                        g.DrawString(d(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r2, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r3, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r4, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][4]), new Font("Arial", f), Brushes.Black, r5, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][5]), new Font("Arial", f), Brushes.Black, r6, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][6]), new Font("Arial", f), Brushes.Black, r7, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][7]), new Font("Arial", f), Brushes.Black, r8, sfo);
                        g.DrawString(d(dts.Rows[linesPrint][8]), new Font("Arial", f), Brushes.Black, r9, sfo);
                        g.DrawString(n(dts.Rows[linesPrint][9]), new Font("Arial", f), Brushes.Black, r10, sfo);

                
                //      String.Format("{0:0.00}", 123.4567);
                        #endregion

                        y += h; linesPrint++; vv.RPT.lp++;
                        if (y >= e.PageBounds.Height - 50) 
                        { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void ost_print(System.Drawing.Printing.PrintPageEventArgs e)
        {



            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            Rectangle r0;
            Rectangle r1, r2, r3, r4, r5,
                      r6, r7, r8,
                      r9, r10;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 220, ww = 30, wk = 55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6 = wk, w7 = wk, w8 = wk + 10,
                w9 = wk, w10 = 15;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Остатки по цеху за период c ", q.tk(ETALON.param.period_s_5), " по ", q.tk(ETALON.param.period_p_6));
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги
            #region r1---r21


            #endregion

            #region drawStr
            //g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            //g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            //g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r0 = new Rectangle(x, y, w0, h);
            r1 = new Rectangle(x + w0, y, w1, h);
            r2 = new Rectangle(x + w0 + w1, y, w2, h);
            r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
            r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
            r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
            r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
            r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
            r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


            g.DrawRectangle(Pens.LightSteelBlue, r0);
            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);
            g.DrawRectangle(Pens.LightSteelBlue, r6);
            g.DrawRectangle(Pens.LightSteelBlue, r7);
            g.DrawRectangle(Pens.LightSteelBlue, r8);
            g.DrawRectangle(Pens.LightSteelBlue, r9);
            g.DrawRectangle(Pens.LightSteelBlue, r10);


            #endregion

            #region drawStr
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r0, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("ОстН", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("Прих", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("Расх", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("ОстНП", new Font("Arial", f), Brushes.Black, r5, sfc);
            g.DrawString("ПрихП", new Font("Arial", f), Brushes.Black, r6, sfc);
            g.DrawString("РасхП", new Font("Arial", f), Brushes.Black, r7, sfc);
            g.DrawString("ОстКП", new Font("Arial", f), Brushes.Black, r8, sfc);
            g.DrawString("ОстФ", new Font("Arial", f), Brushes.Black, r9, sfc);
            g.DrawString("РНС", new Font("Arial", f), Brushes.Black, r10, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                r0 = new Rectangle(x, y, w0, h);
                r1 = new Rectangle(x + w0, y, w1, h);
                r2 = new Rectangle(x + w0 + w1, y, w2, h);
                r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
                r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
                r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
                r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
                r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
                r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
                r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


                g.DrawRectangle(Pens.LightSteelBlue, r0);
                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);
                g.DrawRectangle(Pens.LightSteelBlue, r3);
                g.DrawRectangle(Pens.LightSteelBlue, r4);
                g.DrawRectangle(Pens.LightSteelBlue, r5);
                g.DrawRectangle(Pens.LightSteelBlue, r6);
                g.DrawRectangle(Pens.LightSteelBlue, r7);
                g.DrawRectangle(Pens.LightSteelBlue, r8);
                g.DrawRectangle(Pens.LightSteelBlue, r9);
                g.DrawRectangle(Pens.LightSteelBlue, r10);
                #endregion

                #region drawStr
                //g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                //g.DrawString(String.Format("{0:0.00}", n(dts.Rows[linesPrint][0])), new Font("Arial", f), Brushes.Black, r1);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][1])), new Font("Arial", f), Brushes.Black, r2, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][2])), new Font("Arial", f), Brushes.Black, r3, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][3])), new Font("Arial", f), Brushes.Black, r4, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][4])), new Font("Arial", f), Brushes.Black, r5, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][5])), new Font("Arial", f), Brushes.Black, r6, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][6])), new Font("Arial", f), Brushes.Black, r7, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][7])), new Font("Arial", f), Brushes.Black, r8, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][8])), new Font("Arial", f), Brushes.Black, r9, sfo);
                //g.DrawString(String.Format("{0:C}", n(dts.Rows[linesPrint][9])), new Font("Arial", f), Brushes.Black, r10);



                g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r1);
                g.DrawString(d(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r2, sfo);
                g.DrawString(d(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r3, sfo);
                g.DrawString(d(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r4, sfo);
                g.DrawString(d(dts.Rows[linesPrint][4]), new Font("Arial", f), Brushes.Black, r5, sfo);
                g.DrawString(d(dts.Rows[linesPrint][5]), new Font("Arial", f), Brushes.Black, r6, sfo);
                g.DrawString(d(dts.Rows[linesPrint][6]), new Font("Arial", f), Brushes.Black, r7, sfo);
                g.DrawString(d(dts.Rows[linesPrint][7]), new Font("Arial", f), Brushes.Black, r8, sfo);
                g.DrawString(d(dts.Rows[linesPrint][8]), new Font("Arial", f), Brushes.Black, r9, sfo);
                g.DrawString(n(dts.Rows[linesPrint][9]), new Font("Arial", f), Brushes.Black, r10, sfo);


                //      String.Format("{0:0.00}", 123.4567);
                #endregion

                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void ost_blank_print(System.Drawing.Printing.PrintPageEventArgs e)
        {


            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            Rectangle r0;
            Rectangle r1, r2, r3, r4, r5,
                      r6, r7, r8,
                      r9, r10;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 220, ww = 30, wk = 55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6 = wk, w7 = wk, w8 = wk + 10,
                w9 = wk, w10 = 0;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Остатки по цеху за период c ", q.tk(ETALON.param.period_s_5), " по ", q.tk(ETALON.param.period_p_6));
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги
            #region r1---r21


            #endregion

            #region drawStr
            //g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            //g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            //g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r0 = new Rectangle(x, y, w0, h);
            r1 = new Rectangle(x + w0, y, w1, h);
            r2 = new Rectangle(x + w0 + w1, y, w2, h);
            r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
            r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
            r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
            r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
            r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
            r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


            g.DrawRectangle(Pens.LightSteelBlue, r0);
            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);
            g.DrawRectangle(Pens.LightSteelBlue, r6);
            g.DrawRectangle(Pens.LightSteelBlue, r7);
            g.DrawRectangle(Pens.LightSteelBlue, r8);
            g.DrawRectangle(Pens.LightSteelBlue, r9);
            g.DrawRectangle(Pens.LightSteelBlue, r10);


            #endregion

            #region drawStr
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r0, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("ОстН", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("Прих", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("Расх", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("ОстНП", new Font("Arial", f), Brushes.Black, r5, sfc);
            g.DrawString("ПрихП", new Font("Arial", f), Brushes.Black, r6, sfc);
            g.DrawString("РасхП", new Font("Arial", f), Brushes.Black, r7, sfc);
            g.DrawString("ОстКП", new Font("Arial", f), Brushes.Black, r8, sfc);
            g.DrawString("ОстФ", new Font("Arial", f), Brushes.Black, r9, sfc);
            g.DrawString("", new Font("Arial", f), Brushes.Black, r10, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                r0 = new Rectangle(x, y, w0, h);
                r1 = new Rectangle(x + w0, y, w1, h);
                r2 = new Rectangle(x + w0 + w1, y, w2, h);
                r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
                r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
                r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
                r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
                r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
                r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
                r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);


                g.DrawRectangle(Pens.LightSteelBlue, r0);
                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);
                g.DrawRectangle(Pens.LightSteelBlue, r3);
                g.DrawRectangle(Pens.LightSteelBlue, r4);
                g.DrawRectangle(Pens.LightSteelBlue, r5);
                g.DrawRectangle(Pens.LightSteelBlue, r6);
                g.DrawRectangle(Pens.LightSteelBlue, r7);
                g.DrawRectangle(Pens.LightSteelBlue, r8);
                g.DrawRectangle(Pens.LightSteelBlue, r9);
                g.DrawRectangle(Pens.LightSteelBlue, r10);
                #endregion

                #region drawStr



                g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r1);
                g.DrawString(d(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r2, sfo);
                g.DrawString(d(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r3, sfo);
                g.DrawString(d(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r4, sfo);
                g.DrawString(d(dts.Rows[linesPrint][4]), new Font("Arial", f), Brushes.Black, r5, sfo);
                g.DrawString(d(dts.Rows[linesPrint][5]), new Font("Arial", f), Brushes.Black, r6, sfo);
                g.DrawString(d(dts.Rows[linesPrint][6]), new Font("Arial", f), Brushes.Black, r7, sfo);
                g.DrawString(d(dts.Rows[linesPrint][7]), new Font("Arial", f), Brushes.Black, r8, sfo);
                //g.DrawString("", new Font("Arial", f), Brushes.Black, r9, sfo);
                // g.DrawString("", new Font("Arial", f), Brushes.Black, r10, sfo);


                //      String.Format("{0:0.00}", 123.4567);
                #endregion

                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void ostSum_blank_print(System.Drawing.Printing.PrintPageEventArgs e)
        {


            Graphics g = e.Graphics; string oo = ""; string om = "";
            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;
            
            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            /*
name, pr,
ostn, ostns,
pp, pps,
pol, pols,
pakt, pakts,
rp, rps,
rol, rols,
rakt, rakts,
ostk, ostks,
ct
          
             */
            Rectangle r0, r1,
                      r2, r3, 
                      r4, r5,
                      r6, r7,
                      r8, r9,
                      r10, r11,
                      r12, r13,
                      r14, r15,
                      r16, r17, r18;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 220, ww = 30, wk = 50;
            int w0 = 30;
            int w1 = 220, w2 = wk,
                w3 = wk, w4 = wk, 
                w5 = wk, w6 = wk,
                w7 = wk, w8 = wk ,
                w9 = wk, w10 = wk,
                w11 = wk, w12 = wk,
                w13= wk,w14= wk,
                w15= wk,w16= wk,
                w17 = wk, w18 = wk;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Остатки по цеху за период c ", q.tk(ETALON.param.period_s_5), " по ", q.tk(ETALON.param.period_p_6));
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги

            #region r1---r21

            r0 = new Rectangle(x, y, w0, h);
            r1 = new Rectangle(x + w0, y, w1, h);
            r2 = new Rectangle(x + w0 + w1, y, w2, h);
            r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
            r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
            r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
            r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
            r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
            r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);
            r11 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10, y, w11, h);
            r12 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11, y, w12, h);
            r13 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12, y, w13, h);
            r14 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13, y, w14, h);
            r15 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14, y, w15, h);
            r16 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15, y, w16, h);
            r17 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16, y, w17, h);
            r18 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16 + w17, y, w18, h);
            #endregion

       

            #region drawStr
            object or4 = dts.Compute("sum(ostns)", "ct>0");
            g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r1);
            g.DrawString(or4.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            object or6 = dts.Compute("sum(pps)", "ct>0");
            g.DrawString(or6.ToString(), new Font("Arial", f), Brushes.Black, r6, sfc);
            object or8 = dts.Compute("sum(pols)", "ct>0");
            g.DrawString(or8.ToString(), new Font("Arial", f), Brushes.Black, r8, sfc);
            object or10 = dts.Compute("sum(pakts)", "ct>0");
            g.DrawString(or10.ToString(), new Font("Arial", f), Brushes.Black, r10, sfc);
            object or12= dts.Compute("sum(rps)", "ct>0");
            g.DrawString(or12.ToString(), new Font("Arial", f), Brushes.Black, r12, sfc);
            object or14 = dts.Compute("sum(rols)", "ct>0");
            g.DrawString(or14.ToString(), new Font("Arial", f), Brushes.Black, r14, sfc);
            object or16 = dts.Compute("sum(rakts)", "ct>0");
            g.DrawString(or16.ToString(), new Font("Arial", f), Brushes.Black, r16, sfc);
            object or18 = dts.Compute("sum(ostks)", "ct>0");
            g.DrawString(or18.ToString(), new Font("Arial", f), Brushes.Black, r18, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r0 = new Rectangle(x, y, w0, h);
            r1 = new Rectangle(x + w0, y, w1, h);
            r2 = new Rectangle(x + w0 + w1, y, w2, h);
            r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
            r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
            r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
            r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
            r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
            r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);
            r11 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 +w10, y, w11, h);
            r12 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11, y, w12, h);
            r13 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12, y, w13, h);
            r14 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13, y, w14, h);
            r15 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14, y, w15, h);
            r16 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15, y, w16, h);
            r17 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16, y, w17, h);
            r18 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16 + w17, y, w18, h);



            g.DrawRectangle(Pens.LightSteelBlue, r0);
            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);
            g.DrawRectangle(Pens.LightSteelBlue, r6);
            g.DrawRectangle(Pens.LightSteelBlue, r7);
            g.DrawRectangle(Pens.LightSteelBlue, r8);
            g.DrawRectangle(Pens.LightSteelBlue, r9);
            g.DrawRectangle(Pens.LightSteelBlue, r10);
            g.DrawRectangle(Pens.LightSteelBlue, r11);
            g.DrawRectangle(Pens.LightSteelBlue, r12);
            g.DrawRectangle(Pens.LightSteelBlue, r13);
            g.DrawRectangle(Pens.LightSteelBlue, r14);
            g.DrawRectangle(Pens.LightSteelBlue, r15);
            g.DrawRectangle(Pens.LightSteelBlue, r16);
            g.DrawRectangle(Pens.LightSteelBlue, r17);
            g.DrawRectangle(Pens.LightSteelBlue, r18);

            #endregion

            #region drawStr
            /*select name_cod(name,ct) as name, o, p, r, round(osn,2) as ostn,
             * pp, rp, round(osk,2) as ostk,  
             * round(osk-rad+pad,2) as osf, fpa
 from ost0;*/
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r0, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Цена гр", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("ОстН", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("Прих", new Font("Arial", f), Brushes.Black, r5, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r6, sfc);
            g.DrawString("ПрихДр", new Font("Arial", f), Brushes.Black, r7, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r8, sfc);
            g.DrawString("ПрихАкт", new Font("Arial", f), Brushes.Black, r9, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r10, sfc);
            g.DrawString("Расх", new Font("Arial", f), Brushes.Black, r11, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r12, sfc);
            g.DrawString("РасхДр", new Font("Arial", f), Brushes.Black, r13, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r14, sfc);
            g.DrawString("РасхАкт", new Font("Arial", f), Brushes.Black, r15, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r16, sfc);
            g.DrawString("ОстКон", new Font("Arial", f), Brushes.Black, r17, sfc);
            g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r18, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                r0 = new Rectangle(x, y, w0, h);
                r1 = new Rectangle(x + w0, y, w1, h);
                r2 = new Rectangle(x + w0 + w1, y, w2, h);
                r3 = new Rectangle(x + w0 + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w0 + w1 + w2 + w3, y, w4, h);
                r5 = new Rectangle(x + w0 + w1 + w2 + w3 + w4, y, w5, h);
                r6 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5, y, w6, h);
                r7 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6, y, w7, h);
                r8 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7, y, w8, h);
                r9 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8, y, w9, h);
                r10 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9, y, w10, h);
                r11 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10, y, w11, h);
                r12 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11, y, w12, h);
                r13 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12, y, w13, h);
                r14 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13, y, w14, h);
                r15 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14, y, w15, h);
                r16 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15, y, w16, h);
                r17 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16, y, w17, h);
                r18 = new Rectangle(x + w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15 + w16 + w17, y, w18, h);

                g.DrawRectangle(Pens.LightSteelBlue, r0);
                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);
                g.DrawRectangle(Pens.LightSteelBlue, r3);
                g.DrawRectangle(Pens.LightSteelBlue, r4);
                g.DrawRectangle(Pens.LightSteelBlue, r5);
                g.DrawRectangle(Pens.LightSteelBlue, r6);
                g.DrawRectangle(Pens.LightSteelBlue, r7);
                g.DrawRectangle(Pens.LightSteelBlue, r8);
                g.DrawRectangle(Pens.LightSteelBlue, r9);
                g.DrawRectangle(Pens.LightSteelBlue, r10);
                g.DrawRectangle(Pens.LightSteelBlue, r11);
                g.DrawRectangle(Pens.LightSteelBlue, r12);
                g.DrawRectangle(Pens.LightSteelBlue, r13);
                g.DrawRectangle(Pens.LightSteelBlue, r14);
                g.DrawRectangle(Pens.LightSteelBlue, r15);
                g.DrawRectangle(Pens.LightSteelBlue, r16);
                g.DrawRectangle(Pens.LightSteelBlue, r17);
                g.DrawRectangle(Pens.LightSteelBlue, r18);
                #endregion

                #region drawStr
                


                g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r0);
                g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r1);
                g.DrawString(n(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r2, sfo);
                g.DrawString(d(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r3, sfo);
                g.DrawString(d(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r4, sfo);
                g.DrawString(d(dts.Rows[linesPrint][4]), new Font("Arial", f), Brushes.Black, r5, sfo);
                g.DrawString(d(dts.Rows[linesPrint][5]), new Font("Arial", f), Brushes.Black, r6, sfo);
                g.DrawString(d(dts.Rows[linesPrint][6]), new Font("Arial", f), Brushes.Black, r7, sfo);
                g.DrawString(d(dts.Rows[linesPrint][7]), new Font("Arial", f), Brushes.Black, r8, sfo);
                g.DrawString(d(dts.Rows[linesPrint][8]), new Font("Arial", f), Brushes.Black, r9, sfo);
                g.DrawString(d(dts.Rows[linesPrint][9]), new Font("Arial", f), Brushes.Black, r10, sfo);
                g.DrawString(d(dts.Rows[linesPrint][10]), new Font("Arial", f), Brushes.Black, r11, sfo);
                g.DrawString(d(dts.Rows[linesPrint][11]), new Font("Arial", f), Brushes.Black, r12, sfo);
                g.DrawString(d(dts.Rows[linesPrint][12]), new Font("Arial", f), Brushes.Black, r13, sfo);
                g.DrawString(d(dts.Rows[linesPrint][13]), new Font("Arial", f), Brushes.Black, r14, sfo);
                g.DrawString(d(dts.Rows[linesPrint][14]), new Font("Arial", f), Brushes.Black, r15, sfo);
                g.DrawString(d(dts.Rows[linesPrint][15]), new Font("Arial", f), Brushes.Black, r16, sfo);
                g.DrawString(d(dts.Rows[linesPrint][16]), new Font("Arial", f), Brushes.Black, r17, sfo);
                g.DrawString(d(dts.Rows[linesPrint][17]), new Font("Arial", f), Brushes.Black, r18, sfo);
               
          #endregion

                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void toolStripTextBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (toolStripTextBox2.Text.Length > 0)
            { refresh(toolStripTextBox2.Text); }
            else { refresh(); }
        }

        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            currentPage = 0;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripMenuItem2_Click_fun();
        }
        public void saveDataXLS(string filePath)
        {


            string file_path = filePath;
            int clmns = dataGridView1.Columns.Count;
            Int32 rows = dataGridView1.Rows.Count;
            Int32 i = 0;
            Int32 i1 = 0;
            string clType = "String";//"String";

            string[] aa = new string[clmns];
            for (int j = 0; j < clmns; j++) { aa[j] = dataGridView1.Columns[j].HeaderText; }


            System.IO.StreamWriter file = new System.IO.StreamWriter(file_path);
            file.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            file.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
            file.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
            file.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
            file.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
            file.WriteLine(" <Styles>");
            file.WriteLine("  <Style ss:ID=\"s21\">");
            file.WriteLine("   <NumberFormat ss:Format=\"Fixed\"/>");
            file.WriteLine("  </Style>");
            file.WriteLine(" </Styles>");
            file.WriteLine(" <Worksheet ss:Name=\"Table1\">");
            file.WriteLine("  <Table>");
            file.WriteLine("   <Row>");
            for (i = 0; i < clmns; i++)
            {
                file.WriteLine(string.Concat("    <Cell><Data ss:Type=\"String\">", aa[i], "</Data></Cell>"));
            }
            file.WriteLine("   </Row>");
            for (i1 = 0; i1 < rows; i1++)
            {
                file.WriteLine("   <Row>");
                for (i = 0; i < clmns; i++)
                {
                    file.WriteLine(string.Concat("    <Cell><Data ss:Type=\"", clType, "\">", dataGridView1.Rows[i1].Cells[i].Value, "</Data></Cell>"));
                }
                file.WriteLine("   </Row>");
            }
            file.WriteLine("  </Table>");
            file.WriteLine(" </Worksheet>");
            file.WriteLine("</Workbook>");
            file.Close();
        }

        public void toolStripMenuItem2_Click_fun()
        {
            
            SaveFileDialog SV = new SaveFileDialog();
            SV.Filter = "MSExcel files (*.xls)|*.xls|All files (*.*)|*.*";
            SV.FilterIndex = 1;
            SV.RestoreDirectory = true;
            if (SV.ShowDialog() == DialogResult.OK)
            {
                saveDataXLS(SV.FileName.ToString());
                MessageBox.Show(string.Concat("Экспорт в файл ", SV.FileName.ToString(), " успешно завершен."));
            }
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            RPT_ostS();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            vv.ost_sum_kol.sumTrue_kolFalse = false ;
            loadAndSet();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            vv.ost_sum_kol.sumTrue_kolFalse = true;
            loadAndSet();
        }

       
    }
}
